package com.yz.crm.common.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import com.yz.common.util.JDKStackTrace;
import com.yz.crm.common.dao.CommonDao;

@SuppressWarnings("unused")
public class CommonDaoMybatisImpl<T> implements CommonDao<T> {

    private Logger log = Logger.getLogger(CommonDaoMybatisImpl.class);
    private SqlSessionFactory sessionFactory;

    public SqlSessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SqlSessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public boolean save(T obj) throws Exception {
        SqlSession session = null;
        try {
            session = sessionFactory.openSession();
            session.insert(obj.getClass().getName() + ".insert", obj);
            session.commit();
            return true;
        } catch (Exception e) {
            throw e;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    /**
     * 更新对象,根据主键更新
     *
     * @param Obj
     * @return
     * @throws Exception
     */
    public boolean update(T obj) throws Exception {
        SqlSession session = null;
        try {
            session = sessionFactory.openSession();
            session.update(obj.getClass().getName() + ".updateByPrimaryKey", obj);
            session.commit();
            return true;
        } catch (Exception e) {
            throw e;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public T load(Class<T> clzz, Object id) {
        SqlSession session = null;
        try {
            session = sessionFactory.openSession();
            List<T> list = session.selectList(clzz.getName() + ".loadById", id);
            if (list != null && list.size() > 0) {
                return list.get(0);
            } else {
                return null;
            }
        } catch (Exception e) {
            log.error(JDKStackTrace.getJDKStrack(e));
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return null;
    }

    /**
     * 统计符合查询条件的数据量
     *
     * @param sqlKey
     * @param clzz
     * @param param
     * @return
     */
    public Integer count(String sqlKey, Class<T> clzz, Object param) {
        SqlSession session = null;
        try {
            session = sessionFactory.openSession();
            Object count = session.selectOne(clzz.getName() + "." + sqlKey, param);
            if (count == null) {
                return 0;
            }
            if (count instanceof Integer) {
                return (Integer) count;
            } else if (count instanceof Long) {
                return ((Long) count).intValue();
            }
        } catch (Exception e) {
            log.error(JDKStackTrace.getJDKStrack(e));
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return 0;
    }

    public T queryForObject(String sqlKey, Class<T> clzz, Object param) {
        SqlSession session = null;
        try {
            session = sessionFactory.openSession();
            List<T> list = session.selectList(clzz.getName() + "." + sqlKey, param);
            if (list != null && list.size() > 0) {
                return list.get(0);
            } else {
                return null;
            }
        } catch (Exception e) {
            log.error(JDKStackTrace.getJDKStrack(e));
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return null;
    }

    /**
     * 根据指定查询条件查询符合条件的所有内容列表
     *
     * @param sqlKey
     * @param clzz
     * @param param
     * @return
     */
    public List<T> queryForList(String sqlKey, Class<T> clzz, Map param) {
        SqlSession session = null;
        try {
            session = sessionFactory.openSession();
            List<T> list = session.selectList(clzz.getName() + "." + sqlKey, param);
            return list;
        } catch (Exception e) {
            log.error(JDKStackTrace.getJDKStrack(e));
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return null;
    }

    public List<T> queryForList(String sqlKey, Class<T> clzz, Object obj) {
        SqlSession session = null;
        try {
            session = sessionFactory.openSession();
            List<T> list = session.selectList(clzz.getName() + "." + sqlKey, obj);
            return list;
        } catch (Exception e) {
            log.error(JDKStackTrace.getJDKStrack(e));
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return null;
    }

    /**
     * 逻辑删除单个对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    public boolean deleteLogic(T obj) throws Exception {
        if (obj == null) {
            return false;
        }
        SqlSession session = null;
        try {
            session = sessionFactory.openSession();
            session.update(obj.getClass().getName() + ".deleteLogicById", obj);
            session.commit();
            return true;
        } catch (Exception e) {
            throw e;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    /**
     * 执行指定的sql更新表数据
     *
     * @param sqlKey
     * @param clzz
     * @param param
     * @return
     * @throws Exception
     */
    public boolean updateBySql(String sqlKey, Class clzz, Object param) throws Exception {
        SqlSession session = null;
        try {
            session = sessionFactory.openSession();
            session.update(clzz.getName() + "." + sqlKey, param);
            session.commit();
            return true;
        } catch (Exception e) {
            throw e;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    /**
     * 执行指定的sql删除表数据
     *
     * @param sqlKey
     * @param clzz
     * @param param
     * @return
     * @throws Exception
     */
    public boolean deleteBySql(String sqlKey, Class clzz, Object param) throws Exception {
        SqlSession session = null;
        try {
            session = sessionFactory.openSession();
            session.delete(clzz.getName() + "." + sqlKey, param);
            session.commit();
            return true;
        } catch (Exception e) {
            throw e;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    /**
     * 执行指定的sql逻辑删除表数据
     *
     * @param sqlKey
     * @param clzz
     * @param param
     * @return
     * @throws Exception
     */
    public boolean deleteLogicBySql(String sqlKey, Class clzz, Object param) throws Exception {
        SqlSession session = null;
        try {
            session = sessionFactory.openSession();
            int count = session.update(clzz.getName() + "." + sqlKey, param);
            session.commit();
            return true;
        } catch (Exception e) {
            throw e;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
}
